/** variant.d: The unit test for dotmars/base/variant.d

  Written in the D programming language 1.0

Authors:	Wei Li (oldrev@gmail.com)
Copyright:	Copyright (C) 2007 by Wei Li.
License:	BSD
*/

module test.base.testvariant;

import dotmars.base.variant;
import dotmars.test.core;

class VariantTestCase : TestCase!(VariantTestCase)
{

	this()
	{
		register("testVariant", &testVariant);
		register("testOperators", &testOperators);
		register("testVariantArray", &testVariantArray);
	}

	void testVariant() 
	{
		struct Bar
		{
			public:
				int x = 0;
				int y = 1;
				int z = 2;
		}
		Variant!(int) v0;
		assertTrue(v0.empty);

		Variant!(double, int, char[], Bar) v = 2; // DMD 0.177 new feature
		assertTrue(v == 2);
		assertTrue(v <= 2);
		v = 2.22;
		assertTrue(v == 2.22);
		typeof(v) v2 = v;
		assertTrue(v2 == v);

		Bar b;
		b.x = 10;
		v = b;
		assertTrue(v.get!(Bar)().x == 10);
	}

	void testOperators()
	{
		Variant!(double, int) v0, v1;
		v0 = 128;
		v0 >>= 1;
		assertTrue(v0 == 64);
		v0 *= 2;
		assertTrue(v0 == 128);
		v1 = 1;
		assertTrue(v0 > v1);
		assertTrue(v0 != v1);
		v0 = 5;
		v1 = 4;
		v0 += v1.get!(int);
		assertTrue(v0 == 9);
	}

	void testVariantArray()
	{
		alias Variant!(double, int, char[]) Var;
		Var[] va;
		va ~= Var("AAA".dup);
		
		va ~= Var(12);
		va ~= Var(12.50);

		assertTrue(va[0] == "AAA".dup);
		assertTrue(va[1] == cast(int)12);

	}
}

